package com.huawei.cloud.phone.management.adapter.phone;

import com.huawei.cloud.phone.management.adapter.user.LoginChecker;
import com.huawei.cloud.phone.management.adapter.utils.HttpUtils;
import com.huawei.cloud.phone.management.application.phone.PhoneService;
import com.huawei.cloud.phone.management.application.pkg.dto.PhoneInfoDTO;
import com.huawei.cloud.phone.management.domain.phone.PhoneDomainService;
import com.huawei.cloud.phone.management.domain.phone.UserPhone;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/phone")
public class PhoneController {
    private final PhoneService phoneService;

    private final PhoneDomainService phoneDomainService;

    /**
     * 查询用户手机列表
     *
     * @return 用户手机列表
     */
    @LoginChecker
    @PostMapping(value = "/list")
    public List<UserPhone> listUserPhone() {
        Optional<String> userId = HttpUtils.getUserId();
        if (!userId.isPresent()) {
            return Collections.emptyList();
        }
        log.info("list user phone {}", userId.get());
        return phoneDomainService.getPhones(userId.get());
    }

    /**
     * 同步云手机信息
     * @param phoneInfoDTO 需要同步的云手机信息
     */
    @PostMapping(value = "/sync-info")
    public void syncPhoneInfo(@RequestBody @Valid PhoneInfoDTO phoneInfoDTO) {
        log.info("sync phone info {}", phoneInfoDTO);
        phoneService.syncPhoneInfo(phoneInfoDTO);
    }
}
